package hivi.vn.hivigold.adapters;

import hivi.vn.hivigold.database.WebService;
import hivi.vn.hivigold.objects.Partner;

import java.util.ArrayList;

import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;
import android.widget.SearchView;

public class PartnerLoader extends AsyncTaskLoader<ArrayList<Partner>> {
	ArrayList<Partner> arr;
	Context c;
	SearchView sv;
	public PartnerLoader(Context context, SearchView sv) {
		super(context);
		// TODO Auto-generated constructor stub
		c = context;
		this.sv = sv;
	}

	@Override
	public void deliverResult(ArrayList<Partner> data) {
		// TODO Auto-generated method stub
		if (isReset()) {
			return;
		}
		if (data != null) {
			arr = data;
		}
		super.deliverResult(data);
	}

	@Override
	protected void onReset() {
		// TODO Auto-generated method stub
		super.onReset();
		onStopLoading();
		arr = null;
	}

	@Override
	public ArrayList<Partner> loadInBackground() {
		// TODO Auto-generated method stub
		return WebService.getPartners(c,sv.getQuery().toString());
	}

	@Override
	protected void onStartLoading() {
		// TODO Auto-generated method stub
		if (arr != null) {
			deliverResult(arr);
		}
		if (takeContentChanged() || arr == null) {
			forceLoad();
		}
	}

	@Override
	protected void onStopLoading() {
		// TODO Auto-generated method stub
		cancelLoad();
	}
}
